package com.coupons.business.page.dao;

import java.util.List;

import com.coupons.business.common.exception.BaseCouponsAppException;
import com.coupons.business.common.vo.Page;
import com.coupons.business.common.vo.decorator.PageRowMapper;
import com.coupons.business.util.BaseUtilDAO;

public class PageDAO extends BaseUtilDAO implements IPageDAO {

	public void createPage(Page objaPage) throws BaseCouponsAppException {
		int pageId = getSequenceNextValue(SEQUENCE_NAME);
		Object objParams[] = new Object[] { new Integer(pageId),
				objaPage.getPageName(), objaPage.getDescription() };
		int rtnVal = this.saveOrUpdateOrDelete(
				PAGE_INSERT_STATEMENT.toString(), objParams);
		objaPage.setPageId(pageId);
		System.out.println("executed dao");
		if (rtnVal <= 0) {
			System.out.println("Unable to insert record into Page table");
			return;
		}
	}

	public void updatePage(Page objaPage) throws BaseCouponsAppException {
		Object objParams[] = new Object[] { objaPage.getPageName(),
				objaPage.getDescription(), objaPage.getPageId() };
		int rtnVal = this.saveOrUpdateOrDelete(
				PAGE_UPDATE_STATEMENT.toString(), objParams);
		System.out.println("executed dao");
		if (rtnVal <= 0) {
			System.out.println("Unable to insert record into Page table");
			return;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.coupons.business.merchant.dao.IMerchantDAO#getMerchant(int)
	 * SELECT * FROM PARTNER where MERCHANT_ID = ?
	 */
	public Page getPage(int pageId) throws BaseCouponsAppException {
		try {
			Object objParams[] = new Object[] { new Integer(pageId) };
			List<Object> list = this.findRecords(GET_PAGE_STATEMENT, objParams,
					new PageRowMapper());

			if (null != list && list.size() > 0) {
				return (Page) list.get(0);
			} else
				return null;
		} catch (BaseCouponsAppException e) {
			e.printStackTrace();
			throw e;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.coupons.business.merchant.dao.IMerchantDAO#getAllMerchants()
	 * SELECT * FROM MERCHANT
	 */
	public List<Page> getAllPages() throws BaseCouponsAppException {
		try {
			List list = this.findRecords(GET_ALL_PAGES_STATEMENT,
					new PageRowMapper());
			return list;
		} catch (BaseCouponsAppException e) {
			e.printStackTrace();
			throw e;
		}
	}
}
